package order

import (
	"OnlineRetailers/controllers/errorDeal"
	"OnlineRetailers/controllers/login"
	USER "OnlineRetailers/models/User"
	error2 "OnlineRetailers/models/errorDeal"
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

type GetOrderNum struct {
	beego.Controller
}

func (c *GetOrderNum) GetOrderNum() {
	defer func() {
		c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
		c.Ctx.Output.Status = 200
		c.ServeJSON()
	}()
	var addr = "/Order/GetOrderNum"
	user := &USER.WXUser{}
	log := &error2.Log{}
	var err error
	o := orm.NewOrm()

	fmt.Println(c.Ctx.Request.Header)

	err, openid := login.CheckOpenid(c.Ctx.Request.Header["Openid"])
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}
	user.OpenId = openid

	orderTotal := struct {
		Code  string
		Count []struct {
			Count int
			State int
		}
	}{}

	sql := "select count(*) as count,`state` from `order` where `user_id`=? group by `state`"
	_, err = o.Raw(sql, user.OpenId).QueryRows(&orderTotal.Count)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}

	orderTotal.Code = "200"
	c.Data["json"] = orderTotal

}
